package com.ex.net;

import java.util.HashMap;
import java.util.Map;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.ex.mina.MinaService;
import com.ex.volley.VolleyHelper;
import com.example.androiddemo.Const;
import com.example.androiddemo.R;
import com.example.ui.base.BaseFragment;

/**
 * 网络示例：http、socket
 * @author thduan
 * 2015-9-17 14:49:32
 */
public class NetFragment extends BaseFragment implements OnClickListener{
	private TextView res;
	private EditText editText;
	private SocketDemo socketDemo;
	
	private MinaService minaService;

	@Override
	public int getLayoutId() {
		return R.layout.net_fragment;
	}

	@Override
	public void initialViews(View root, LayoutInflater inflater) {
		initParams();
		
		res = (TextView) root.findViewById(R.id.res);
		editText = (EditText) root.findViewById(R.id.editText);
		
		root.findViewById(R.id.httpBtn).setOnClickListener(this);
		root.findViewById(R.id.socketSendBtn).setOnClickListener(this);
		root.findViewById(R.id.socketInitBtn).setOnClickListener(this);
		root.findViewById(R.id.socketCloseBtn).setOnClickListener(this);
		
		//mina
		root.findViewById(R.id.minaInit).setOnClickListener(this);
		root.findViewById(R.id.minaSend).setOnClickListener(this);
		root.findViewById(R.id.minaClose).setOnClickListener(this);
	}
	
	private void initParams() {
		minaService = new MinaService();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		if(socketDemo != null) {
			socketDemo.closeServer();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.httpBtn: //http请求
			req();
			break;
		case R.id.socketInitBtn: //socket 初始化
			if(socketDemo != null) {
				socketDemo.closeServer();
				socketDemo = null;
			}
			socketDemo = new SocketDemo();
			socketDemo.setListener(getSocketListener());
			break;
		case R.id.socketSendBtn://socket 发送
			if(socketDemo != null) {
				socketDemo.sendMsgReq(editText.getText().toString(), null);
//				for(int i = 0; i < 15; i++) {
//					socketDemo.sendMsgReq(editText.getText().toString(), null);
//				}
			}
			break;
		case R.id.socketCloseBtn: //socket 关闭服务
			if(socketDemo != null) {
				socketDemo.closeServer();
			}
			break;
			
		case R.id.minaInit: //mina 连接server
			minaService.startConn();
			break;
		case R.id.minaSend: //mina发送消息
			minaService.sendMsg(editText.getText().toString());
			break;
		case R.id.minaClose: //mina关闭
			minaService.closeService();
			break;

		default:
			break;
		}
	}
	
	private SocketListener getSocketListener() {
		return new SocketListener() {
			
			@Override
			public void onReceive(String msg) {
				res.setText(msg);
			}
		};
	}
	
	private String resText = "";
	
	private void req() {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
//				httpTest();
				volleyTest();
			}
		}).start();
	}
	
	/**
	 * volley 框架测试
	 */
	private void volleyTest() {
//		VolleyDemo.stringReq(Const.HTTP_POST_URL);
//		VolleyDemo.stringReqPost(Const.HTTP_POST_URL);
		VolleyHelper.reqPost(createTestParam());
	}
	
	/**
	 * 创建post请求参数
	 * @return
	 */
	private Map<String, String> createTestParam() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("userName", "xiongxiong");
		map.put("age", "5");
		return map;
	}
	
	/**
	 * http请求测试
	 */
	private void httpTest() {
//		resText = HttpUtils.httpReq(url);
//		resText = HttpUtils.androidHttpClientReq(url);
//		resText = HttpUtils.httpClientReq(url);
//		resText = HttpUtils.javaNetPostParam(url);
//		resText = HttpUtils.uploadFile(Const.HTTP_UPLOAD_URL);
//		resText = HttpUtils.httpUrlConnUpload(Const.HTTP_UPLOAD_URL);
//		resText = DownloadUtil.clientDownload(Const.HTTP_DOWNLOAD_URL);
		resText = DownloadUtil.httpUrlConnDownload(Const.HTTP_DOWNLOAD_URL);
		_instance.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				res.setText(resText);
			}
		});
	}
	
}
